﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using danreng.Application;
using danreng.Domain;
using danreng.Host.Helper;
using danreng.Host.Models;
using Microsoft.Practices.Unity;
using sharp_net.Mvc;

namespace danreng.Host.Controllers {
    public class AccountController : Controller {

        [Dependency]
        public IUserService userApp { get; set; }
        [Dependency]
        public IFormsAuthentication authentication { get; set; }

        public ActionResult LogOn() {
            string name = User.Identity.Name;
            if (!string.IsNullOrEmpty(name))
                return RedirectToAction("Use", "Seat");
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl) {
            if (!ModelState.IsValid)
                return View(model);
            UserData user = userApp.GetByLogin(model.Email, model.Password);
            if (user == null) {
                ModelState.AddModelError("", "提供的用户名或密码不正确。");
                return View(model);
            }
            authentication.SetAuthCookie(this.HttpContext, AuthTicket.CreateTicket(user));
            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1)
                return Redirect(returnUrl);
            else
                return RedirectToAction("Use", "Seat");
        }

        public ActionResult LogOff() {
            Session.Abandon();
            authentication.Signout();
            return RedirectToAction("LogOn", "Account");
        }

        public ActionResult Register() {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel model) {
            if (!ModelState.IsValid)
                return View(model);
            string error = string.Empty;
            if (userApp.CheckUserExist(new UserData { Email = model.Email }, ref error)) {
                ModelState.AddModelError("", "用户名已存在。");
                return View(model);
            }
            UserData user = new UserData {
                Email = model.Email,
                PasswordHash = Domain.User.EncryptPassword(model.Password)
            };
            userApp.Creat(user);
            authentication.SetAuthCookie(this.HttpContext, AuthTicket.CreateTicket(user));
            return RedirectToAction("Home", "Index");
        }

        public ActionResult ChangePassword() {
            return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model) {
            if (!ModelState.IsValid)
                return View(model);
            UserIdentity user = (UserIdentity)ViewBag.UserModel;
            UserData userCheck = userApp.GetByLogin(user.Email, model.OldPassword);
            if (userCheck == null) {
                ModelState.AddModelError("", "当前密码不正确或新密码无效。");
                return View(model);
            }
            UserData userNew = new UserData {
                Id = user.Id,
                PasswordHash = Domain.User.EncryptPassword(model.NewPassword)
            };
            userApp.Update(userNew);
            return RedirectToAction("ChangePasswordSuccess");
        }

        public ActionResult ChangePasswordSuccess() {
            return View();
        }

    }
}
